<?php

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * JsLogic возвращает закодированную javascript - логику для страниц
 *
 * @author yuriy
 */
class Shrinker_Model_JsCode {
    
    public function getIndexJs(){
    
    $code =  '$(\'div.shrink div.nspr.inneri.nblu input\').fadeTo(0,.7);
	$(\'div.shrink div.nspr.inneri.nblu\').hover(function(){$(\'input\',this).fadeTo(400,.9);},
											   function(){$(\'input\',this).fadeTo(400,.7);});
	var _adb_url = $(\'div.shrink input[name="adb_url"]\');
	_adb_url.val(\'http://\')//.watermark(\'http://\')
			.focus(function(){$(\'div.shrink div.outeri\').addClass(\'over\');})
			.bind(\'blur change\',function(){$(\'div.shrink div.outeri\').removeClass(\'over\')}).width(545);
			
	var _updateLinkEvents = function() {
		$.ajaxSetup({cache:false});
		$(\'table.links_table tr[id^="lid"] a#edit\').click(function() {
			var _e = $(this).parent().parent();
			var _t = _e.attr(\'id\').substr(4);
			var spanrtitle = $(\'span#rtitle\',_e), spanlong = $(\'span#long\',_e), tdadtype = $(\'td#adtype\',_e), _tdlast = $(\'td:last\',_e);
			var adtype = tdadtype.text();
			spanrtitle.css(\'display\', \'block\')
				.html(\'<span style="width:50px;display:inline-block;font-size:12px;">Title:</span><input type="text" name="title" size="30" value="\'+spanrtitle.text()+\'" />\');
			spanlong.html(\'<span style="width:50px;display:inline-block;font-size:12px;">Website:</span><input type="text" name="website" size="30" value="\'+spanlong.text()+\'" />\');
			tdadtype.html(\'<select name="adtype">\'
						  + \'<option value="1"\'+(adtype==\'Interstitial\'?\' selected\':\'\')+\'>Interstitial</option>\'
						  + \'<option value="2"\'+(adtype==\'Top Banner\'?\' selected\':\'\')+\'>Top Banner</option>\'
						  + \'<option value="3"\'+(adtype==\'None\'?\' selected\':\'\')+\'>None</option>\'
						  + \'</select>\');
			var edit_btn = $(\'<input type="submit" value="Edit" />\');
			_tdlast.children().hide();
			_tdlast.append(edit_btn);
			edit_btn.click(function() {
				var website = $(\'input[name="website"]\',_e).val(), title = $(\'input[name="title"]\',_e).val(), 
					adtype = $(\'select[name="adtype"]\',_e).val();
				$.get(\'ajax.php\',{opt:\'edit_link\',args:{lid:_t,website:website,title:title,adtype:adtype}},
					function(r) {
						var j = eval(\'(\' + r + \')\');
						if (j.error) {
							alert(j.error);
						} else if (j.message) {
							spanrtitle.html(\'<em>\' + title + \'</em>\');
							spanlong.html(website);
							tdadtype.html(adtype==1?\'Interstitial\':(adtype==2?\'Top Banner\':\'None\'));
							edit_btn.remove();
							_tdlast.children().show();
						} else {
							alert(\'[0x1el01] Error Communicating With Server - Please try again\');
						}
					}
				);
				return false;
			});
			return false;
		});
		$(\'table.links_table tr[id^="lid"] a#rem\').click(function() {
			var e = $(this).parent().parent();
			var t = e.attr(\'id\').substr(4);
			if (confirm(\'Are you sure you want to delete this link?\')) {
				$.get(\'ajax.php\',{opt:\'rem_link\',args:{lid:t}},
					function(r) {
						var j = eval(\'(\' + r + \')\');
						if (j.error) {
							alert(j.error);
						} else if (j.message) {
							e.remove();
							//$(\'table.links_table\').trigger(\'change\');
						} else {
							alert(\'[0x1rl01] Error Communicating With Server - Please try again\');
						}
					}
				);
			}
			return false;
		});
	};
	
	var _updateLinks = function() {
		var lt = $(\'table.links_table tbody\');
		$.ajaxSetup({cache:false});
		$.get(\'ajax.php\',{opt:\'get_links\'}, function(r) {
			var j = eval(\'(\' + r + \')\');
			if (j.message) {
				lt.html(j.message.data).ready(function() {
					_updateLinkEvents();
					$(\'div#_lp\').html(j.message.lp);
				});
			} else {
				lt.html(\'<tr id="no_link"><td colspan="5"><em>No data available</em></td></tr>\');
			}
		});
	}; _updateLinks();
	
	/*ZeroClipboard.setMoviePath(\'js/ZeroClipboard.swf\');
	var zclip = new ZeroClipboard.Client();
	zclip.addEventListener(\'mouseDown\',function(){zclip.setText(_adb_url.val());});*/
	
	$(\'div.shrink form\').submit(function() {
		var s = $(\'input[type="image"]\', this), t = _adb_url;
		switch (s.attr(\'value\')) {
			case \'COPY\': break;
			default:
				var tval = t.val();
				t.attr(\'readonly\', \'1\');
				t.val(\'Укорачиваю...\');
				var args = {adtype:$(\'select[name="advert_type"]\',this).val(),
							title:$(\'input[name="custom_name"]\',this).val()};
				$.post(\'shrinker/async/getshortlink\',{opt:\'shrink\',url:encodeURI(tval),args:args},
					function(r) {
						var j = eval(\'(\' + r + \')\');
						if (j.error) {
							t.removeAttr(\'readonly\');
							t.val(tval);
							alert(j.error);
						} else if (j.message) {
							t.val(j.message);
							var do_another = $(\'<span class="do_another">Добавить другую</span>\');
							t.width(400).parent().append(do_another);
							do_another.click(function() {
								_adb_url.removeAttr(\'readonly\').val(\'http://\').width(545);
								do_another.remove();
								s.attr(\'value\', \'SHRINK!\').attr(\'id\', \'\');
                                                                s.attr(\'src\', \'layouts/shrinker/css/images/search-btns.png\');
								zclip.destroy();
								return false;
							});
							s.attr(\'value\', \'COPY\');
                                                        s.attr(\'src\', \'layouts/shrinker/css/images/copy.png\');
							s.attr(\'id\', \'zero_copy\');
							zclip.glue(\'zero_copy\');
							_updateLinks();
						} else {
							alert(\'[0x1sh01] Ошибка соединения с сервером - Пожалуйста, попробуйте еще раз.\');
						}
					}
				);
				break;
		}
		return false;
	});
	//$(\'table.links_table\').change(function() { _updateLinks(); });
	
	var $ml_options = $(\'div.ml_options_box\');
	var ml_options_height = $ml_options.height();
	$ml_options.hide().height(0);
	$(\'div.ml_options div.nspr.outeri\').hover(
		function(){$(\'div.ml_options div.nspr.outeri\').toggleClass(\'over\');}
	).click(function() {
		if ($ml_options.is(\':visible\')) {
			$ml_options.animate({ height: 0 }, { duration: 300, complete: function () {
				$ml_options.hide();
			} });
			$(this).text(\'More Options\');
		} else {
			$ml_options.show().animate({ height: ml_options_height }, { duration: 300 });
			$(this).text(\'Less Options\');
		}
	});
	
	';
    
    return $this->JSPack($code);
    }
    
//    public function getIndexJs(){
//        
//        return indexPage();
//        
//}
    

    private function JSPack($js, $encoding = 10, $fastDecode = true) {
		$jsp = new Shrinker_Model_JavaScriptPacker($js, $encoding, $fastDecode, false);
		return $jsp->pack();
}
}
?>
